package com.ai.academiainteligente;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ai.WS.AIWS;
import com.ai.WS.AIWSTask;
import com.ai.util.AIActivity;
import com.ai.util.AISession;
import com.ai.util.PlanoExercicio;
import com.ai.util.PlanoExercicioAdapter;
import com.ai.util.User;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class PlanoExercicioList extends Activity implements
		OnItemClickListener, AIActivity {

	ArrayList<PlanoExercicio> planosExercicio;
	ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_plano_exercicio_list);
		User usuario = AISession.usuario;
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		if (usuario.getAcademias().size() > 1) {
			// manda escolher a academia
		} else {
			ListarPlanoExercicio(usuario.getId(), usuario.getAcademias().get(0)
					.getId());
		}
		ListView listaPlanoExercicio = (ListView) findViewById(R.id.listPlanoExercicio);
		listaPlanoExercicio.setOnItemClickListener(this);
	}

	public void ListarPlanoExercicio(int idCliente, int idAcademia) {
		AIWSTask conserveTask = new AIWSTask(this,
				AIWS.metodos.GetPlanoExercicios, progressBar);
		try {
			conserveTask.execute(idCliente + "", idAcademia + "");
		} catch (Exception e) {
			conserveTask.cancel(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.plano_exercicio_list, menu);
		return true;
	}

	@Override
	public void retornoWS(JSONObject json) {
		try {

			if (json.getInt("code") == 407) {
				planosExercicio = new ArrayList<PlanoExercicio>();
				JSONArray array = json.getJSONArray("plano_exercicios");
				for (int i = 0; i < array.length(); i++) {
					JSONObject objeto = array.getJSONObject(i);
					PlanoExercicio planoExercio = new PlanoExercicio();
					planoExercio.setSerie(objeto.getString("serie"));
					planoExercio.setMaquina(objeto.getString("maquina"));
					planoExercio.setModalidade(objeto.getString("modalidade"));
					planoExercio.setExercicio(objeto.getString("exercicio"));
					planoExercio.setTempo(objeto.getString("tempo"));
					planoExercio.setQuantidade(objeto.getString("quantidade"));
					planoExercio.setData(objeto.getString("data"));
					planoExercio.setId(objeto.getInt("id"));
					planoExercio.setIdPlanoExercicioFrequencia(objeto
							.getInt("idPlanoExercicioFrequencia"));
					planoExercio.setConcluido(objeto.getBoolean("concluido"));
					planosExercicio.add(planoExercio);
					ListView grid = (ListView) findViewById(R.id.listPlanoExercicio);
					grid.setAdapter(new PlanoExercicioAdapter(planosExercicio));
				}
			} else {
				Toast.makeText(this, "Plano Concluido", Toast.LENGTH_SHORT)
						.show();
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// Intent intent = new Intent(this, PontoActivity.class);
		// intent.putExtra("Category", categorias.get(position));
		// intent.putExtra("lat", getIntent().getDoubleExtra("lat", 0));
		// intent.putExtra("lng", getIntent().getDoubleExtra("lng", 0));
		// st/artActivityForResult(intent, WelcomeActivity.NOVO_CONSERVE);
		CheckBox box = (CheckBox) view.findViewById(R.id.ckbConcluido);
		box.setChecked(true);

	}

	@Override
	public Activity getActivity() {
		return this;
	}

}
